<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SGoods extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    /**
     * 查询参数数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalGoodsList($where, $field,$limit,$page){
        $order = [
            "order_num" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 根据$id获取商品数据
     * @param $id
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsByIdFind($id,$field){
        if(empty($id)){
            return false;
        }
        $where = [
            "goods_id" =>$id,
        ];
        $result = $this->where($where)->field($field)->find();
        return $result;
    }

    public function DelGoodsIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['goods_id', 'in', $ids],
            ['del_flag', '=', 0]
        ];
        $data = [
            'del_flag'=>2
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function getNormalGoodsCount($where){
        $count = $this->where($where)->count();
        return $count;
    }

    /**
     * 增加商品数据值
     * @param $goodsId
     * @param $field
     * @param $num
     * @return mixed
     */
    public function setIncGoodsData($goodsId,$field,$num){
        $where = [
            ['goods_id', '=', $goodsId],
        ];
        $result = $this->where($where)->inc($field, $num)->update();
        return $result;
    }

    /**
     * 减少商品数据值
     * @param $goodsId
     * @param $field
     * @param $num
     * @return mixed
     */
    public function setDecGoodsData($goodsId,$field,$num){
        $where = [
            ['goods_id', '=', $goodsId],
        ];
        $result = $this->where($where)->dec($field, $num)->update();
        return $result;
    }

    /**
     * 更改商品状态
     * @param $userId
     * @param $status
     * @return bool
     */
    public function changeStatusGoodsModel($goodsId,$status){
        if(empty($goodsId)){
            return false;
        }
        $where = [
            ['goods_id', '=', $goodsId]
        ];
        $data = [
            'status'=>$status
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }
}
